package crm.copy;

public class CommandHandle {
	String kind;
	/**
	 * 对command进行初步判断，进入不同的方法接口处理
	 * @param command
	 * @return
	 */
	public String commandHandle(String command){
		String[] tempCommand=command.split(" ");
		switch(tempCommand[0]){
		case "L"   : this.handleL(tempCommand); break;
		case "A"   : this.handleA(tempCommand); break;
		case "D"   : this.handleD(tempCommand); break;
		case "U"   : this.handleU(tempCommand); break;
		case "O"   : this.handleO(tempCommand); break;
		case "H"   : this.handleH(tempCommand); break;
		case "HELP": this.handleH(tempCommand); break;
		case "Q"   : this.handleQ(tempCommand); break;
		default: kind="None";System.out.println("The command you input is error!");break;
		}
		return kind;
	}
	
	public void handleL(String[] tempCommand){
		if(tempCommand.length==1)
		{
            kind = "L";
		}
		else {
			if(tempCommand[1].equals("id")&&IsInt.isInt(tempCommand[2]))
			{
				kind = "L id";
			}
			else if(tempCommand[1].equals("name"))
			{
				kind = "L name";
			}
			else
			{
				System.out.println("The command you input is wrong···················");
				System.out.println("Input [L id id_num]/[L name nameString]··················");
				kind = "L";
			}
		}
	}
	
	public void handleA(String[] tempCommand){
		if(tempCommand.length>=2){
			String[] newAdd=tempCommand[1].split(",");
			if(newAdd.length>=1&&newAdd[0].split(":").length==2&&newAdd[0].split(":")[0].equals("name"))
			   kind =  "A" ;
			else
			{
				System.out.println("The command you input is error!");
				System.out.println("A command fromat: A name:xxx,sex:xxx,birthday:xxx,address:xxx,call:xxx");
				kind = "None";
			}
		}
		else
		{
			System.out.println("The command you input is error!");
			System.out.println("A command fromat: A name:xxx,sex:xxx,birthday:xxx,address:xxx,call:xxx");
			kind = "None";
		}
			  
//		if(newAdd.length==5)
//		{
//			String[] strTag=new String[5] ;
//			String[] strContent=new String[5];
//			for(int i=0;i<5;i++)
//			{
//				strTag[i]=newAdd[i].split(":")[0];
//				strContent[i]=newAdd[i].split(":")[1];
//				System.out.println(strTag[i]+"\""+strContent[i]);
//			}
//			if(strTag[0].equals("name")&&strTag[1].equals("sex")&&strTag[2].equals("birthday")&&strTag[3].equals("address")&&strTag[4].equals("call"))
//			{
//				if(!strContent[0].equals(""))  //姓名不为空
//				{
//					kind = "A";
//				}
//				else 
//					kind = "None";
//			}
//			else 
//				kind = "None";
//
//		}
//		else 
//			kind = "None";
	}
	
	public void handleD(String[] tempCommand){
	    if(IsInt.isInt(tempCommand[1]))
		{
			kind = "D id";
		}
	    else
		{
			System.out.println("The command you input is error!");
			System.out.println("D command fromat: D id  //such as:D 17····");
			kind = "None";
		}
	}
	
	public void handleU(String[] tempCommand){
		if(IsInt.isInt(tempCommand[1]))
		{
			kind = "U id";
//			String[] newAdd=tempCommand[2].split(",");
//			if(newAdd.length==5)
//			{
//				String[] strTag = new String[5] ;
//				String[] strContent = new String[5] ;
//				for(int i=0;i<5;i++)
//				{
//					strTag[i]=newAdd[i].split(":")[0];
//					strContent[i]=newAdd[i].split(":")[1];
//				}
//				if(strTag[0].equals("name")&&strTag[1].equals("sex")&&strTag[2].equals("birthday")&&strTag[3].equals("address")&&strTag[4].equals("call"))
//				{
//					if(!strContent[0].equals(""))  //姓名不为空
//					{
//						kind = "U id";
//					}
//					else 
//						kind = "None";
//				}
//				else 
//					kind = "None";
//
//			}
//			else 
//				kind = "None";
		}
		else
		{
			System.out.println("The command you input is error!");
			System.out.println("U command fromat: U id name:xxx,sex:xxx,birthday:xxx,address:xxx,call:xxx ");
			kind = "None";
		}
		
	}
	
	public void handleO(String[] tempCommand){
		if(tempCommand[1].equals("id"))
		{
			kind = "O id";
		}
		else if(tempCommand[1].equals("name"))
		{
			kind = "O name";
		}
		else
		{
			System.out.println("The command you input is error!");
			System.out.println("O command fromat: O id/name");
			kind = "None";
		}
	}
	
	public void handleH(String[] tempCommand){
		Help.helpHandle(tempCommand);
		kind="H";
	}
	
	public void handleQ(String[] tempCommand){
		kind="Q";
		System.out.println("exit the crm!");
		System.exit(1);
	}
	
	
}
